"""
@author: 江同学呀
@file: ip_field.py
@date: 2024/7/27 20:20
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/ip.html

    IPv4 and IPv6 addresses.
    IPv4 和 IPv6 地址。
"""
from abc import ABC
from typing import Type, Union, Any, Dict

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import BoostAttribute, DocValuesAttribute, IgnoreMalformedAttribute, \
    IndexAttribute, NullValueAttribute, OnScriptErrorAttribute, ScriptAttribute, StoreAttribute, \
    TimeSeriesDimensionAttribute
from espc.utils.build_add_attr import build_add_attr


class IPField(
    _BaseField, BoostAttribute, DocValuesAttribute, IgnoreMalformedAttribute, IndexAttribute, NullValueAttribute,
    OnScriptErrorAttribute, ScriptAttribute, StoreAttribute, TimeSeriesDimensionAttribute, ABC
):
    """
    IP 字段
    """
    _field_type: str = "ip"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None, boost: float = None,
            doc_values: bool = None, ignore_malformed: bool = None, index: bool = None, null_value: Any = None,
            store: bool = None, on_script_error: str = None, script: str = None, time_series_dimension: bool = None,
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, boost=boost, doc_values=doc_values,
            ignore_malformed=ignore_malformed, index=index, null_value=null_value, on_script_error=on_script_error,
            script=script, store=store, time_series_dimension=time_series_dimension
        )
        return

    def __get__(self, instance: _Base, owner: Type["IPField"]) -> Union["IPField", str]:
        return super(IPField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(IPField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> str:
        return self._value

    @value.setter
    def value(self, value: str):
        self._value: str = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


